﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace TimKen.DataAccess
{
    public class PCategoryDAO
    {
        public PCategoryDAO()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int CategoryID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@CategoryID", SqlDbType.Int,4)};
            parameters[0].Value = CategoryID;

            int result = DbHelperSQL.RunProcedure("UP_PCategory_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public void Add(TimKen.DataEntity.PCategory model)
        {

            SqlParameter[] parameters = {
					new SqlParameter("@CategoryID", SqlDbType.Int,4),
					new SqlParameter("@LanguageID", SqlDbType.Int,4),
					new SqlParameter("@CategoryImage", SqlDbType.VarChar,64),
					new SqlParameter("@ParentID", SqlDbType.Int,4),
					new SqlParameter("@SortOrder", SqlDbType.Int,4),
					new SqlParameter("@DateAdded", SqlDbType.DateTime),
					new SqlParameter("@LastModified", SqlDbType.DateTime),
					new SqlParameter("@CategoryName", SqlDbType.VarChar,100),
                    new SqlParameter("@IsLeaf", SqlDbType.Bit,1),
                    	new SqlParameter("@Description", SqlDbType.NVarChar,500)
                                        };
            parameters[0].Value = model.CategoryID;
            parameters[1].Value = model.LanguageID;
            parameters[2].Value = model.CategoryImage;
            parameters[3].Value = model.ParentID;
            parameters[4].Value = model.SortOrder;
            parameters[5].Value = model.DateAdded;
            parameters[6].Value = model.LastModified;
            parameters[7].Value = model.CategoryName;
            parameters[8].Value = model.IsLeaf;
            parameters[9].Value = model.Description;

            DbHelperSQL.RunProcedure("UP_PCategory_ADD", parameters);
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.PCategory model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@CategoryID", SqlDbType.Int,4),
					new SqlParameter("@LanguageID", SqlDbType.Int,4),
					new SqlParameter("@CategoryImage", SqlDbType.VarChar,64),
					new SqlParameter("@ParentID", SqlDbType.Int,4),
					new SqlParameter("@SortOrder", SqlDbType.Int,4),
					new SqlParameter("@DateAdded", SqlDbType.DateTime),
					new SqlParameter("@LastModified", SqlDbType.DateTime),
					new SqlParameter("@CategoryName", SqlDbType.VarChar,100),
                    new SqlParameter("@IsLeaf", SqlDbType.Bit,1),
                    	new SqlParameter("@Description", SqlDbType.NVarChar,500)};
            parameters[0].Value = model.CategoryID;
            parameters[1].Value = model.LanguageID;
            parameters[2].Value = model.CategoryImage;
            parameters[3].Value = model.ParentID;
            parameters[4].Value = model.SortOrder;
            parameters[5].Value = model.DateAdded;
            parameters[6].Value = model.LastModified;
            parameters[7].Value = model.CategoryName;
            parameters[8].Value = model.IsLeaf;
            parameters[9].Value = model.Description;

            DbHelperSQL.RunProcedure("UP_PCategory_Update", parameters, out rowsAffected);
        }

        //更改IsLeaf的状态
        public void ChangeIsLeaf(int CategoryID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@CategoryID", SqlDbType.Int,4)};
            parameters[0].Value = CategoryID;
            DbHelperSQL.RunProcedure("UP_PCategory_ChangeIsLeaf", parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int CategoryID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@CategoryID", SqlDbType.Int,4)};
            parameters[0].Value = CategoryID;

            DbHelperSQL.RunProcedure("UP_PCategory_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.PCategory GetModel(int CategoryID, int LanguageID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@CategoryID", SqlDbType.Int,4),
					new SqlParameter("@LanguageID", SqlDbType.Int,4)};
            parameters[0].Value = CategoryID;
            parameters[1].Value = LanguageID;

            TimKen.DataEntity.PCategory model = new TimKen.DataEntity.PCategory();
            DataSet ds = DbHelperSQL.RunProcedure("UP_PCategory_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["CategoryID"].ToString() != "")
                {
                    model.CategoryID = int.Parse(ds.Tables[0].Rows[0]["CategoryID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LanguageID"].ToString() != "")
                {
                    model.LanguageID = int.Parse(ds.Tables[0].Rows[0]["LanguageID"].ToString());
                }
                model.CategoryImage = ds.Tables[0].Rows[0]["CategoryImage"].ToString();
                if (ds.Tables[0].Rows[0]["ParentID"].ToString() != "")
                {
                    model.ParentID = int.Parse(ds.Tables[0].Rows[0]["ParentID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SortOrder"].ToString() != "")
                {
                    model.SortOrder = int.Parse(ds.Tables[0].Rows[0]["SortOrder"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DateAdded"].ToString() != "")
                {
                    model.DateAdded = DateTime.Parse(ds.Tables[0].Rows[0]["DateAdded"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LastModified"].ToString() != "")
                {
                    model.LastModified = DateTime.Parse(ds.Tables[0].Rows[0]["LastModified"].ToString());
                }

                if (ds.Tables[0].Rows[0]["IsLeaf"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsLeaf"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsLeaf"].ToString().ToLower() == "true"))
                    {
                        model.IsLeaf = true;
                    }
                    else
                    {
                        model.IsLeaf = false;
                    }
                }
                model.CategoryName = ds.Tables[0].Rows[0]["CategoryName"].ToString();
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CategoryID,LanguageID,CategoryImage,ParentID,SortOrder,DateAdded,LastModified,CategoryName,IsLeaf,Description ");
            strSql.Append(" FROM PCategory ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListAndKwd(int languageID)
        {
            return GetListAndKwd(languageID, 0);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListAndKwd(int languageID,int ParentID)
        {
            SqlParameter[] parameters = 
            { 
                new SqlParameter("@LanguageID", SqlDbType.Int, 4),
                new SqlParameter("@ParentID", SqlDbType.Int, 4)
            };
            parameters[0].Value = languageID;
            parameters[1].Value = ParentID;
            return DbHelperSQL.RunProcedure("UP_PCategory_GetListAndKwd", parameters, "PCategory");
        }

        /// <summary>
        /// 获得热门词
        /// </summary>
        public DataSet GetPHotKeyWords(int languageID, int ParentID)
        {
            SqlParameter[] parameters = 
            { 
                new SqlParameter("@LanguageID", SqlDbType.Int, 4),
                new SqlParameter("@ParentID", SqlDbType.Int, 4)
            };
            parameters[0].Value = languageID;
            parameters[1].Value = ParentID;
            return DbHelperSQL.RunProcedure("UP_PCategory_GetPHotKeyWords", parameters, "PCategory");
        }

        /// <summary>
        /// 设置热门词
        /// </summary>
        public bool SetPHotKeyWords(string action, int sort, int languageID, int CategoryID)
        {
            int rowsAffected;
            SqlParameter[] parameters = 
            { 
                new SqlParameter("@Action", SqlDbType.NVarChar, 10),
                new SqlParameter("@Sort", SqlDbType.Int, 4),
                new SqlParameter("@LanguageID", SqlDbType.Int, 4),
                new SqlParameter("@CategoryID", SqlDbType.Int, 4)
            };
            parameters[0].Value = action;
            parameters[1].Value = sort;
            parameters[2].Value = languageID;
            parameters[3].Value = CategoryID;
            int i = DbHelperSQL.RunProcedure("UP_PCategory_SetPHotKeyWords", parameters, out rowsAffected);
            if (i > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetTreeGrid(int languageID, int ParentID)
        {
            SqlParameter[] parameters = 
            { 
                new SqlParameter("@LanguageID", SqlDbType.Int, 4),
                new SqlParameter("@ParentID", SqlDbType.Int, 4)
            };
            parameters[0].Value = languageID;
            parameters[1].Value = ParentID;
            return DbHelperSQL.RunProcedure("UP_PCategory_GetTreeGrid", parameters, "PCategory");
        }

        public DataSet GetCategoryPageList(int pageSize, int PageIndex, int parentID, int languageID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@ParentID", SqlDbType.Int,4),
					new SqlParameter("@PageSize", SqlDbType.Int,4),
                    new SqlParameter("@PageIndex", SqlDbType.Int,4),
                    new SqlParameter("@LanguageID",SqlDbType.Int,4)};
            parameters[0].Value = parentID;
            parameters[1].Value = pageSize;
            parameters[2].Value = PageIndex;
            parameters[3].Value = languageID;
            return DbHelperSQL.RunProcedure("UP_PCategory_GetPageList", parameters, "PCategory");

        }

        /// <summary>
        /// 获取满足条件的数据数量
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        public long GetListCount(string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "PCategory";
            parameters[1].Value = "CategoryID";
            parameters[2].Value = 1;
            parameters[3].Value = 0;
            parameters[4].Value = strWhere;
            SqlDataReader dr = DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters);
            if (dr.Read())
            {
                long o = Convert.ToInt64(dr[0]);
                dr.Close();
                dr.Dispose();
                return o;
            }
            return 0;

        }


        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("CategoryID", "PCategory");
        }

        #region 获取所有的父类
        /// <summary>
        /// 返回产品分类所有父类
        /// </summary>
        /// <param name="categoryID">分类ID</param>
        /// <param name="languageID">语言ID</param>
        /// <returns>返回结构 categoryname:categoryid,categoryname:categoryid,categoryname:categoryid,</returns>
        public string GetAllParent(int categoryID, int languageID)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@CategoryID", SqlDbType.Int),
                    new SqlParameter("@LanguageID", SqlDbType.Int)
                                       };
            parameters[0].Value = categoryID;
            parameters[1].Value = languageID;
            SqlDataReader dr = DbHelperSQL.RunProcedure("UP_PCategory_GetAllParent", parameters);
            if (dr.Read())
            {
                string parent = dr[0].ToString();

                dr.Close();
                dr.Dispose();
                return parent;

            }
            return "";
        }
        #endregion

        #region
        ///<summary>返回分类的子列表</summary>
        /// <param name="categoryID">分类ID</param>
        /// <param name="languageID">语言ID</param>
        /// <returns>返回结构 categoryname:categoryid,categoryname:categoryid,categoryname:categoryid,</returns>
        public DataSet GetChild(int categoryID, int languageID, int brandID)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@CategoryID", SqlDbType.Int),
                    new SqlParameter("@LanguageID", SqlDbType.Int),
                    new SqlParameter("@BrandID",SqlDbType.Int)
                                       };
            parameters[0].Value = categoryID;
            parameters[1].Value = languageID;
            parameters[2].Value = brandID;
            DataSet ds = DbHelperSQL.RunProcedure("UP_PCategory_GetChild", parameters, "ChildCategory");
            return ds;
        }
        #endregion

        #region
        ///<summary>返回分类的子列表</summary>
        /// <param name="categoryID">分类ID</param>
        /// <param name="languageID">语言ID</param>
        /// <returns>返回结构 categoryname:categoryid,categoryname:categoryid,categoryname:categoryid,</returns>
        public DataSet CategoryListSite(int cid, int languageID)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@cid", SqlDbType.Int),
                    new SqlParameter("@LanguageID", SqlDbType.Int)
            };
            parameters[0].Value = cid;
            parameters[1].Value = languageID;
            DataSet ds = DbHelperSQL.RunProcedure("UP_Proc_CategoryListSite", parameters, "ChildCategory");
            return ds;
        }
        #endregion

        #endregion  成员方法
    }

}
